package com.epoint.mobileoa.bizlogic.mobileoa;

import java.util.HashMap;
import java.util.List;

import android.os.Message;

import com.epoint.mobileoa.bizlogic.ConfigKey;
import com.epoint.mobileoa.bizlogic.FrameMethod;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.datanalysis.XMLHelp;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.http.HttpHelp;
import com.epoint.mobileoa.model.Ou;
import com.epoint.mobileoa.model.User;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.DateHelp;
import com.epoint.mobileoa.tools.HtmlHelp;
import com.epoint.mobileoa.tools.LogHelp;
import com.epoint.mobileoa.tools.StringHelp;

/**
 * @author liyc
 * @time 2012-2-22 下午3:33:54
 * @annotation 
 */
public class SynchroOrganizationPagingTask extends Task {

	public SynchroOrganizationPagingTask(SuperView con,
			HashMap<String, Object> param) {
		super(con, param);
		
	}

	@Override
	public void exectue(Message mess) throws Exception {
		long beginTime = System.currentTimeMillis();
		
//		LogHelp.Log2SDInfo("开始分页同步人员!");
		String seamurl = getCon().fp.getSeamurl()+"?requestType=GetDSUserAndOuNums";
		String bs = HttpHelp.getHttpBack(seamurl);
		MainService.check_bs(bs);
		int allusers = Integer.parseInt(StringHelp.getXMLAtt(bs, "allusernums"));
		int allous = Integer.parseInt(StringHelp.getXMLAtt(bs, "allounums"));
//		LogHelp.Log2SDInfo("同步人员总数为"+allusers);
//		LogHelp.Log2SDInfo("同步部门总数为"+allous);
		int pagenum = 5000;
		int userpages = allusers/pagenum+1;
		DBHelp.deleteUsers(getCon());
		for(int i=0;i<userpages;i++){
			int showpageindex = i+1;
//			LogHelp.Log2SDInfo("同步人员第"+showpageindex+"页");
			seamurl = getCon().fp.getSeamurl()+"?requestType=DSUsersPaging&pagenum="+pagenum+"&showpageindex="+showpageindex;
			bs = HttpHelp.getHttpBack(seamurl);
			List<User> ulist = XMLHelp.XML2UserList(bs);
//			LogHelp.Log2SDInfo("实际数据:"+ulist.size()+"条");
			DBHelp.setUserListPaging(getCon(), ulist);
		}
		
//		LogHelp.Log2SDInfo("开始分页同步部门!");
		DBHelp.deleteOus(getCon());
		int oupages = allous/pagenum+1;
		for(int i=0;i<oupages;i++){
			int showpageindex = i+1;
//			LogHelp.Log2SDInfo("同步部门第"+showpageindex+"页");
			seamurl = getCon().fp.getSeamurl()+"?requestType=DSDeptsPaging&pagenum="+pagenum+"&showpageindex="+showpageindex;
			bs = HttpHelp.getHttpBack(seamurl);
			List<Ou> olist = XMLHelp.XML2OuList(bs);
//			LogHelp.Log2SDInfo("实际数据:"+olist.size()+"条");
			DBHelp.setOuListPaging(getCon(), olist);
		}
		long time = System.currentTimeMillis() - beginTime;
		String dsperson = StringHelp.getHtmlFormat1(getCon(),"同步人：", getCon().fp.getDisplayname());
		String dsip = StringHelp.getHtmlFormat1(getCon(),"同步IP：",FrameMethod.getIp(getCon().fp, getCon()));
		String dstime = StringHelp.getHtmlFormat1(getCon(),"同步日期：", DateHelp.getCurrentTime());
		String dscostime = StringHelp.getHtmlFormat1(getCon(),"同步耗时：", time/1000+"秒");
		String dsusernums = StringHelp.getHtmlFormat1(getCon(),"人员信息：", allusers+"");
		String dsounums = StringHelp.getHtmlFormat1(getCon(),"部门信息：", allous+"");
		String dsinfo = HtmlHelp.HL+dsperson+HtmlHelp.HL+dsip+HtmlHelp.HL+dstime+HtmlHelp.HL+dscostime+HtmlHelp.HL+dsusernums+HtmlHelp.HL+dsounums+HtmlHelp.HL;
		DBHelp.setConfigValue(getCon(), ConfigKey.dsinfo, dsinfo);
		setMessageToRefreshUI(mess, getCon(), "<result>true</result><info>"+dsinfo+"</info>");
	}

}
